package com.gdth.widget.listview.adapter;

import java.util.List;

import com.gdth.R;
import com.gdth.model.DeleteItem;
import com.gdth.utils.ImageUtil;
import com.gdth.widget.listview.adapter.SlideView.OnSlideListener;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

/** 
 * @author 作者 Arvin: 
 * @version 创建时间：2015年7月9日 上午11:27:13 
 * 可删除的listview适配器 
 */
public class SlideAdapter extends BaseAdapter{
	private Context mContext;
	
	private int layout = R.layout.aron_listview_item;
	
	/**
	 * 数据集
	 */
	private List<DeleteItem> items;
	
	/**
	 * 滑动事件
	 */
	private OnSlideListener onSlideListener;
	/**
	 * 删除事件
	 */
	private OnClickListener onDeleteClickListener;
	
    public SlideAdapter(Context mContext, List<DeleteItem> items, OnSlideListener onSlideListener, OnClickListener onDeleteClickListener) {
        super();
        
        this.onSlideListener = onSlideListener;
        this.onDeleteClickListener = onDeleteClickListener;
        this.items = items;
        this.layout = R.layout.aron_listview_item;
        
        this.mContext = mContext;
    }

    @Override
    public int getCount() {
        return items.size();
    }

    @Override
    public Object getItem(int position) {
        return items.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        SlideView slideView = (SlideView) convertView;
        if (slideView == null) {
            View itemView = LayoutInflater.from(mContext).inflate(layout, null);

            slideView = new SlideView(this.mContext);
            slideView.setContentView(itemView);

            holder = new ViewHolder();
            
            holder.localImageView = (ImageView) itemView.findViewById(R.id.listview_item_img);
		    holder.localTextView = (TextView) itemView.findViewById(R.id.listview_item_txt);
		    holder.deleteHolder = slideView;
            
            slideView.setOnSlideListener(onSlideListener);
            slideView.setTag(holder);
        } else {
            holder = (ViewHolder) slideView.getTag();
        }
        DeleteItem item = items.get(position);
        item.slideView = slideView;
        item.slideView.shrink();

        if (holder != null){
			if (item.getImg() != null){
		    	ImageUtil.downloadAsyncTask(holder.localImageView, item.getImg());
		    	
		    }
			holder.localTextView.setText(item.getValue());
		}
        
        if (onDeleteClickListener != null)
        	holder.deleteHolder.setOnClickListener(new DeleteClickListener(position));
        else
        	holder.deleteHolder.setOnClickListener(onDeleteClickListener);
        
        return slideView;
    }

    /**
     * 删除事件
     * @author lenovo
     *
     */
    private class DeleteClickListener implements OnClickListener{
    	private int position;
    	
    	public DeleteClickListener(int position){
    		this.position = position;
    	}
    	
		@Override
		public void onClick(View arg0) {
			// TODO Auto-generated method stub
			items.remove(position);
			
			SlideAdapter.this.notifyDataSetChanged();
	    	Toast.makeText(mContext, "删除第" + position+"个条目", Toast.LENGTH_LONG).show();
		}
    }
    
    private static class ViewHolder {
        public ImageView localImageView;
        public TextView localTextView;
        public ViewGroup deleteHolder;//(ViewGroup)view.findViewById(R.id.holder);
    }
}
